#!/bin/bash
#
# Startup script for Kloxo-MR
#
# chkconfig: - 85 15
# description: The Kloxo-MR is Web hosting control panel
# processname: kloxo
# config: /etc/httpd/conf/kloxo.conf
# pidfile: /var/run/kloxo-hiawatha.pid
#

# Source function library.
. /etc/rc.d/init.d/functions

# Source networking configuration.
. /etc/sysconfig/network

# Check that networking is up.
[ "$NETWORKING" = "no" ] && exit 0

INIT_DIR='/usr/local/lxlabs/kloxo/init'

if [ ! -d ${INIT_DIR} ] ; then
	mkdir -p ${INIT_DIR}
	chown -R lxlabs:lxlabs ${INIT_DIR}
fi

PID_DIR=${INIT_DIR}

if [ ! -d ${PID_DIR} ] ; then
	mkdir -p ${PID_DIR}
	chown -R lxlabs:lxlabs ${PID_DIR}
fi

if [ ! -f ${INIT_DIR}/webused.txt ] ; then
	echo 'hiawatha' > ${INIT_DIR}/webused.txt
fi

WEBUSED=$(cat ${INIT_DIR}/webused.txt)

### 1. PHP-CGI portion

if [ -f /usr/local/lxlabs/kloxo/init/php_active ] ; then
	mv -f /usr/local/lxlabs/kloxo/init/php_active /usr/local/lxlabs/kloxo/init/kloxo_php_active
fi

php_active=$(cat /usr/local/lxlabs/kloxo/init/kloxo_php_active)

if [ "${1}" == "start" ] ; then
	sh /script/fixlxphpexe ${php_active}
else
	if [ ! -f /usr/local/lxlabs/kloxo/init/kloxo_php_active ] ; then
		sh /script/fixlxphpexe
		php_active=$(cat /usr/local/lxlabs/kloxo/init/kloxo_php_active)
	fi
fi

if [ "${php_active}" == "php52s" ] || [ "${php_active}" == "php52m" ] ; then
	phpcgi_file="/opt/${php_active}/custom/php-cgi.sh"
else
	phpcgi_file="/opt/${php_active}/custom/php-fpm.sh"
fi

phpcgi_conf="${INIT_DIR}/kloxo-php-fpm.conf"

phpcgi_ini="/opt/${php_active}/custom/php.ini"

cd /usr/local/lxlabs/kloxo/httpdocs
lxphp.exe -f ../init/changeport.php

phpcgi_pidfile="${INIT_DIR}/kloxo-phpcgi.pid"
phpcgi_symlink="${INIT_DIR}/kloxo-phpcgi"
phpcgi_user="lxlabs"
phpcgi_child="4"
phpcgi_max_req="10000"

phpcgi_name=$(basename ${phpcgi_symlink})

phpcgi_sockfile="${INIT_DIR}/${phpcgi_name}.sock"

phpcgi_lockfile="/var/lock/subsys/${phpcgi_name}"

phpcgi_prog="kloxo-phpcgi"
prog_as_slave="/usr/local/lxlabs/${prog}/etc/conf/slave-db.db"
prog_wrapper="kloxo-wrapper.sh"
wrapper_pidfile="/usr/local/lxlabs/kloxo/pid/wrapper.pid"

if [ "$(rpm -qa spawn-fcgi)" == "" ] ; then
	yum install spawn-fcgi -y
fi

ln -sf ${phpcgi_file} ${phpcgi_symlink}

if [ "${php_active}" == "php52s" ] || [ "${php_active}" == "php52m" ] ; then
	phpcgi_spawn="/usr/bin/spawn-fcgi -f ${phpcgi_symlink} -s ${phpcgi_sockfile} \
		-C ${phpcgi_child} -u ${phpcgi_user} -g ${phpcgi_user}  -P $phpcgi_pidfile \
		> /dev/null 2>&1"
else
	phpcgi_spawn="${phpcgi_symlink} -y ${phpcgi_conf} -c ${phpcgi_ini} -R"
fi

function phpcgi_start() {
	phpcli_load_wrapper

	if [ -f ${phpcgi_pidfile} ] ; then
		phpcgi_stop
	fi

	if [ ! -f /usr/local/lxlabs/kloxo/init/kloxo_use_php-cgi ] ; then
		echo -n $"Starting ${phpcgi_prog} ('${php_active}' in 'fpm' mode): "

		daemon ${phpcgi_spawn}

		retval=$?
		echo
		[ ${retval} -eq 0 ] && touch ${phpcgi_lockfile} && touch ${phpcgi_sockfile}
		return ${retval}
	else
		echo $"* ${phpcgi_prog} ('${php_active}' in 'cgi' mode): No need to start"
	fi
}

function phpcgi_stop() {
	if [ ! -f /usr/local/lxlabs/kloxo/init/kloxo_use_php-cgi ] ; then
		echo -n $"Stopping ${phpcgi_prog}: "

		## hidden for wrapper only
		killproc -p ${wrapper_pidfile} >/dev/null 2>&1
		killproc -p ${phpcgi_pidfile} -d 10

		retval=$?
		echo
		[ ${retval} -eq 0 ] && rm -f ${phpcgi_lockfile} && rm -f ${phpcgi_sockfile}
		return ${retval}
	fi
}

function phpcgi_restart() {
	phpcgi_stop
	phpcgi_start

	also_mysql
}

function phpcgi_reload() {
	if [ ! -f /usr/local/lxlabs/kloxo/init/kloxo_use_php-cgi ] ; then
		echo -n $"Reloading ${phpcgi_prog}: "
		killproc -p ${phpcgi_pidfile} -d 10 ${phpcgi_name} -USR2
		RETVAL=$?
		echo
	fi

	also_mysql
}

function phpcli_load_wrapper() {
	cd /usr/local/lxlabs/kloxo/httpdocs
	chmod 755 ../sbin/kloxo-wrapper.sh
	../sbin/kloxo-wrapper.sh &
}

function phpcgi_rh_status() {
	if [ ! -f /usr/local/lxlabs/kloxo/init/kloxo_use_php-cgi ] ; then
		status ${phpcgi_name}
	fi
}

function phpcgi_rh_status_q() {
	phpcgi_rh_status >/dev/null 2>&1
}

### 2.1. HIAWATHA portion

if [ -f /opt/hiawatha/usr/sbin/hiawatha ] ; then
	hiawatha_prog="/opt/hiawatha/usr/sbin/hiawatha"
	wigwam_prog="/opt/hiawatha/usr/sbin/wigwam"
else
	hiawatha_prog="/usr/sbin/hiawatha"
	wigwam_prog="/usr/sbin/wigwam"
fi

hiawatha_symlink="${INIT_DIR}/kloxo-hiawatha"
wigwam_symlink="${INIT_DIR}/kloxo-wigwam"
hiawatha_name=$(basename ${hiawatha_symlink})
wigwam_name=$(basename ${wigwam_symlink})

hiawatha_conf=${INIT_DIR}

hiawatha_pidfile="${INIT_DIR}/kloxo-hiawatha.pid"
hiawatha_lockfile="/var/lock/subsys/kloxo-hiawatha"

if [ ! -f ${hiawatha_prog} ]; then
	echo -e "${hiawatha_prog} not found."
	exit 1;
else
	sh /script/hiawatha-off
fi

function hiawatha_start() {
	if [ ! -L ${hiawatha_symlink} ] ; then
		ln -s ${hiawatha_prog} ${hiawatha_symlink}
	fi
	
	if [ "$(ps aux |grep 'kloxo-hiawatha'|grep 'lxlabs')" != "" ] ; then
		hiawatha_stop
	fi

	echo -n "Starting ${hiawatha_name}: "
	daemon "${hiawatha_symlink} -c ${hiawatha_conf}"

	retval=$?
	echo
	[ ${retval} -eq 0 ] && touch ${hiawatha_lockfile}
	return ${retval}
}

function hiawatha_stop() {
	if [ "$(ps aux |grep 'kloxo-hiawatha'|grep 'lxlabs')" == "" ] ; then
		return 1
	fi

	echo -en "Stopping ${hiawatha_name}: "

	killproc ${hiawatha_name}

	retval=$?
	echo
	[ ${retval} -eq 0 ] && rm -f ${hiawatha_lockfile}
	return ${retval}
}

function hiawatha_restart() {
	hiawatha_stop
	hiawatha_start
}

function hiawatha_reload() {
	echo -n $"Reloading ${hiawatha_name}: "
	killproc ${hiawatha_name} -USR1
	RETVAL=$?
	echo
	return $RETVAL
}

function hiawatha_config_check() {
	echo -e "Configuration check via ${wigwam_name}..."
	${wigwam_symlink} -c ${hiawatha_conf}
	echo
	echo -e "Configuration check via ${hiawatha_name}..."
	${hiawatha_symlink} -c ${hiawatha_conf} -k
}

function hiawatha_status() {
	pidnum=$(cat ${hiawatha_pidfile})

	if [ -f $hiawatha_pidfile ] && ps ${pidnum} >/dev/null 2>&1 ; then
		echo -e "${hiawatha_name} (pid  ${pidnum}) is running..."
	else
		echo -e "${hiawatha_name} is not running..."
	fi
}

### 2.2. NGINX portion

### 2.3. LIGHTTPD portion

### 3. GENERAL portion

function also_mysql() {
	if [ ! -f ${prog_as_slave} ] ; then
		if [ -f /etc/initd/mysql ] ; then
			if ! $(/etc/init.d/mysql status) ; then
				/etc/init.d/mysql start
			fi
		elif [ -f /etc/initd/mysqld ] ; then
			if ! $(/etc/init.d/mysqld status) ; then
				/etc/init.d/mysqld start
			fi
		fi

		sleep 1
	fi
}

function install_hiawatha() {
	if [ ! -f /usr/sbin/hiawatha ] ; then
		reinstall_hiawatha
	fi
}

function reinstall_hiawatha() {
	if [ ! -f /usr/sbin/hiawatha ] ; then
		YUM_ACTION='reinstall'
	else
		YUM_ACTION='install'
	fi

	yum ${YUM_ACTION} hiawatha -y
	chkconfig hiawatha off
}

### 4. SELECT portion

case "$1" in
	start)
		phpcgi_$1
		${WEBUSED}_$1
		;;
	stop)
		phpcgi_$1
		${WEBUSED}_$1
		;;
	restart)
		phpcgi_$1
		${WEBUSED}_$1
		;;
	reload)
		phpcgi_$1
		${WEBUSED}_$1
		;;
	wrapper)
		phpcli_load_wrapper
		;;
	update)
		reinstall_${WEBUSED}
		;;
	status)
		phpcgi_rh_$1
		${WEBUSED}_status
		;;
	*)
		echo $"Usage: $0 {start|stop|status|restart|reload|update}"
		exit 2
esac
